/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace DaveSexton.DocToMaml
{
	internal sealed class ProgramServices : IServiceProvider, IDisposable
	{
		#region Public Properties
		public UIService UIService
		{
			get
			{
				if (uiService == null)
					uiService = new UIService(owner);

				return uiService;
			}
		}
		#endregion

		#region Private / Protected
		private UIService uiService;
		private readonly ProgramForm owner;
		private readonly ServiceContainer services;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ProgramServices" /> class.
		/// </summary>
		public ProgramServices(ProgramForm owner)
		{
			if (owner == null)
				throw new ArgumentNullException("owner");

			this.owner = owner;

			services = new ServiceContainer(this);
		}
		#endregion

		#region IServiceProvider Members
		public object GetService(Type serviceType)
		{
			if (serviceType == null)
				return null;
			else if (serviceType == typeof(IServiceContainer))
				return services;
			else if (serviceType == typeof(IUIService))
				return UIService;
			// TODO: Support more service types
			else
			{
				ISite site = owner.Site;

				if (site != null)
					return site.GetService(serviceType);
				else
					return null;
			}
		}
		#endregion

		#region IDisposable Members
		/// <summary>
		/// Releases all resources used by an instance of the <see cref="ProgramServices" /> class.
		/// </summary>
		/// <remarks>
		/// This method calls the virtual <see cref="Dispose(bool)" /> method, passing in <strong>true</strong>, and then suppresses 
		/// finalization of the instance.
		/// </remarks>
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		/// <summary>
		/// Releases unmanaged resources before an instance of the <see cref="ProgramServices" /> class is reclaimed by garbage collection.
		/// </summary>
		/// <remarks>
		/// This method releases unmanaged resources by calling the virtual <see cref="Dispose(bool)" /> method, passing in <strong>false</strong>.
		/// </remarks>
		~ProgramServices()
		{
			Dispose(false);
		}

		/// <summary>
		/// Releases the unmanaged resources used by an instance of the <see cref="ProgramServices" /> class and optionally releases the managed resources.
		/// </summary>
		/// <param name="disposing"><strong>true</strong> to release both managed and unmanaged resources; <strong>false</strong> to release only unmanaged resources.</param>
		private void Dispose(bool disposing)
		{
			if (disposing)
				services.Dispose();
		}
		#endregion
	}
}
